package com.ccx.coachsite.ui.login;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.GetVerifyCodeBus;
import com.ccx.coachsite.ui.login.RegisterActivity.OnGetVerify;
import com.ccx.coachsite.util.CommonViewUtils;
import com.ccx.coachsite.util.ContactsUtil;
import com.ccx.coachsite.util.StringUtil;

public class ForgetPwdActivity extends CommonActivity {

	private EditText mobileEdit;
	private View mSaveBtn; // 保存

	private String mobile; // 手机号
	
	private View mVerifyBtn; // 获取验证
	private EditText mVerityCode; // 验证码
	Animation shake;
	
	@Override
	protected void initComponents() {
		// TODO Auto-generated method stub
		mobileEdit = (EditText) findViewById(R.id.mobile);
		mobileEdit.setText(mobile);
		
		mVerityCode = (EditText) findViewById(R.id.verify_code);

		mSaveBtn = findViewById(R.id.login_top_right);
		mSaveBtn.setOnClickListener(new OnSaveClickListener());
		
		mVerifyBtn = findViewById(R.id.repassword_btn_verify);
		mVerifyBtn.setOnClickListener(new OnGetVerify());
	}
	
	class OnGetVerify implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (shake == null)
				shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);

			String mobilenumber = mobileEdit.getText().toString().trim();
			if (StringUtil.isEmpty(mobilenumber)) {
				CommonViewUtils.showErrorByEditText(mobileEdit,
						getString(R.string.register_mobile_empty), shake);
				return;
			}
			
			if(!StringUtil.isEmpty(mobilenumber) && (mobilenumber.length() < 11 || !ContactsUtil.checkContact(mobilenumber))) {
				CommonViewUtils.showErrorByEditText(mobileEdit,
						"请输入正确的天翼手机号码", shake);
				return ;
			}
			if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
				Toast.makeText(mContext, R.string.login_network_error,
						Toast.LENGTH_SHORT).show();
				return;
			}

			GetVerifyCodeBus bo = new GetVerifyCodeBus(ForgetPwdActivity.this);
			bo.getVerifyCode(mobilenumber);
		}
	}
	
	class OnSaveClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);
			String mobile = mobileEdit.getText().toString().trim();
			final String verifyCode = mVerityCode.getText().toString().trim();
			
			if (StringUtil.isEmpty(mobile)) {
				CommonViewUtils.showErrorByEditText(mobileEdit, getString(R.string.forget_mobile_empty), shake);
				return;
			}
			if(!StringUtil.isEmpty(mobile) && (mobile.length() < 11 || !ContactsUtil.checkContact(mobile))) {
				CommonViewUtils.showErrorByEditText(mobileEdit,
						"请输入正确的天翼手机号码", shake);
				return ;
			}
			
			if (StringUtil.isEmpty(verifyCode)) {
				CommonViewUtils.showErrorByEditText(mVerityCode,
						getString(R.string.register_verify_code_empty), shake);
				return;
			}
			
			if (!CommonViewUtils.isNetConnectionAvailable(mContext)) {
				Toast.makeText(mContext, R.string.login_network_error, Toast.LENGTH_SHORT).show();
				return;
			} else {

			}
			save(mobile, verifyCode);
		}
	}

	private void save(String mobile, String code) {
		CommonBus bo = null;
		if (bo == null) {
			bo = new CommonBus(mContext);
		}

		bo.setForgetPwdParam(mobile, code);
		bo.doTask();
	}
	
	@Override
	protected int getMainContentViewId() {
		return R.layout.activity_forget_password;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected void setCustomTitleTop() {
		TopViewUtil.updateRightTitle(this, R.id.login_top_right, R.string.forget_save);
		TopViewUtil.updateRight(this, R.id.login_top_right,R.drawable.login_save_btn);
		TopViewUtil.updateTitle(this, R.id.login_top_title, R.string.forget);
		TopViewUtil.setRightVisable(this,  R.id.login_top_right, View.VISIBLE);
	}

	@Override
	protected void initData() {
	}
}
